/**
 *
 * Copyright 2008 davethedev@googlemail.com
 * 
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package nethelp.network;

import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.swing.table.AbstractTableModel;
import javax.naming.directory.*;

public class MXRecordTableModel extends AbstractTableModel {

    public Attribute attrs;
    private String columnNames[] = { "Priority", "Mail Host" };
    private Collection<MXRecord> mxRecordList = null;

    public MXRecordTableModel(Attribute attrs) {
        try {
            this.attrs = attrs;
            mxRecordList = new ArrayList<MXRecord>();
            for (int row = 0; row < attrs.size(); row++) {
                MXRecord record = new MXRecord(attrs.get(row).toString());
                mxRecordList.add(record);
            }

        } catch (NamingException ex) {
            Logger.getLogger(MXRecordTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getRowCount() {
        return attrs.size();
    }

    public int getColumnCount() {
        return 2;
    }

    public Object getValueAt(int row, int col) {
        MXRecord record = (MXRecord) mxRecordList.toArray()[row];
        if (col == 0) {
            return record.getPriority();
        } else {
            return record.getMailHost();
        }
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }
}
